<?php include 'header.php';?>
    <!--其他区域-->
    <section>
        <div class="input-search">
            <input type="text" style="padding:0;padding-right:15%" placeholder="search..." class="button mcolor" id="search" value="<?php if(isset($content)){echo $content;}else{echo '';} ?>">
            <div class="back-icon" onclick="gosearch()"><i id="search-icon" class="icon iconfont icon-search"></i></div>
        </div>
        <p style="padding-left:20px;color:#9a9a9a">搜索结果</p>
        <hr>
        <div id="booklist">
            
            <?php
                if( count($search_result) == 0 ){
                    echo '<div style="font-size:20px;width:100%;padding-top:30px;text-align:center;">';
                    echo '    T.T,什么都没搜到呢';
                    echo '</div>';
                }else{
                    foreach( $search_result as $key => $val ){
                        $temp_key = $key+1;
                        echo '<a href="/detail?href='.$val['href'].'" class="item block">';
                        echo '<h3>'.$val['name'].'</h3>';
                        echo '<p><span>索引号: '.$val['index'].'</span></p>';
                        echo '<p><span>作&nbsp;&nbsp;&nbsp;者: '.$val['author'].'</span></p>';
                        echo '<p><span>出版社: '.$val['press'].'</span></p>';
                        echo '<p><span style="color:#329da3;">'.$val['collect'].'&nbsp;&nbsp;&nbsp;'.$val['rent'].'</span></p>';
                        echo '</a>';
                    }
                }
                
            ?>            
        </div>
    </section>
<?php include 'footer.php';?>
<script type="text/javascript">
    //topbar
    $("#topbar").html('<a onclick="history.go(-1)" style="color:#fff;"><h1><i class="icon iconfont icon-left" style="margin-right:4px;"></i>搜索</h1></a>');
    var page = 2,isajax=0,pageall; 
    function getmore(){
        if (isajax==1) {
            if (pageall && page >= pageall) {
                toast('木有更多啦...');
            };
            return;
        };
        $("#loading").show();
        $.ajax({
            type:"get",
            url:"api/lib/search?content="+$("#search").val()+"&page="+page,
            dataType:"json",//text/xml/json，非必须
            success:function(data){//回调函数，非必须
                $("#loading").hide();
                if (!data.error) {
                    // 是否只有一页数据
                    pageall = data.page.all;
                    if (page > pageall) {
                        toast('木有更多啦...');
                        isajax=1;
                        return;
                    };
                    isajax=1;
                    page++;
                    var temp='';
                    for(var i=0;i<data.result.length;i++){
                        var tpl = '<a href="/detail?href='+data.result[i].href+'" class="item block">\
                                        <h3>'+data.result[i].name+"</h3>\
                                        <p><span>索引号：</span>"+data.result[i].index+"</p>\
                                        <p><span>作&nbsp;&nbsp;&nbsp;者：</span>"+data.result[i].author+"</p>\
                                        <p><span>出版社：</span>"+data.result[i].press+"</p>\
                                    </a>";
                        temp = temp+tpl;
                    }
                    $("#booklist").append(temp);

                    isajax=0;
                    console.log(data);
                }else{
                    toast(data.message);
                };
            }
        });     
    }
    $(window).scroll(function () {
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight ==  scrollHeight) {
            getmore(page);
        }
    });
    function gosearch(){
        if ($("#search").val()=='') {
            toast("搜索内容不能为空！");
        }else{
            location.href="/search?content="+encodeURIComponent($("#search").val());
        };
    }
</script>